<?php
/**
 * 用户登录认证
 * User: dxk
 * Date: 2020/07/15
 */

namespace App\Service\Auth;

use App\Contract\Auth;
use App\Model\Core\User\UserModel;
use App\Service\Core\User\User\UserService;

class AppAuthService implements Auth
{
    /**
     * 登录
     * @param $user_id
     * @return array
     */
    public static function login($user_id): array
    {
        if (!is_numeric($user_id) || $user_id <= 0) {
            return alert_info(1, '用户信息错误');
        }
        $user_id = (int)$user_id;
        $check_res = UserService::getInstance()->checkUserUsable($user_id);
        if ($check_res['code'] !== 0) {
            return $check_res;
        }
        $user_info = $check_res['data']['user_info'];
        UserModel::getInstance()->updateOne($user_id,
            [
                'pre_login_at' => $user_info['last_login_at'],
                'last_login_at' => date('Y-m-d H:i:s'),
                'last_login_ip' => easy_ip()
            ]);
        return alert_info(0, '登录成功', $user_info);
    }
}
